package com.example.miaopin;

import java.util.HashMap;
import java.util.Set;

import com.example.miaopin.fragment.DashijieF;
import com.example.miaopin.fragment.DingshiS;
import com.example.miaopin.fragment.HushuaL;
import com.example.miaopin.fragment.HushuaL.RefreshLayout;
import com.example.miaopin.fragment.MiaopingF;
import com.example.miaopin.utils.SPUtil;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author Administrator 主activity
 */
public class MainActivity extends FragmentActivity implements OnClickListener, RefreshLayout {

	private static final String APP_ID = "1104794063";
	private MiaopingF miaopingF;
	private HushuaL dingshiF;
	private DingshiS hushuaF;
	private DashijieF dashijieF;
	private HashMap<Integer, Fragment> fragments;
	private FrameLayout container;
	private ImageView[] mTabs;
	private int index;
	private int lastTabIndex;
	private int currentTabIndex;
	private String[] fragmentTags = new String[] { "0", "1", "2", "3" };
	private int callbaclCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (savedInstanceState != null) {
			callbaclCount = savedInstanceState.getInt("callbaclCount");
			Log.i("ainio", "callbaclCount" + callbaclCount);
			if (callbaclCount == 2) {
				savedInstanceState = null;
			}
		} else {
			callbaclCount = 0;
		}
		initTab();
		initView();
	}

	private void initTab() {
		fragments = null;
		supportFragmentManager = getSupportFragmentManager();
		miaopingF = (MiaopingF) supportFragmentManager.findFragmentByTag(fragmentTags[0]);
		if (miaopingF == null) {
			miaopingF = new MiaopingF();
		}

		dingshiF = (HushuaL) supportFragmentManager.findFragmentByTag(fragmentTags[1]);
		if (dingshiF == null) {
			dingshiF = new HushuaL();
		}
		dingshiF.setmRefreshLayout(this);
		hushuaF = (DingshiS) supportFragmentManager.findFragmentByTag(fragmentTags[2]);
		if (hushuaF == null) {
			hushuaF = new DingshiS();
		}
		dashijieF = (DashijieF) supportFragmentManager.findFragmentByTag(fragmentTags[3]);
		if (dashijieF == null) {
			dashijieF = new DashijieF();
		}
		fragments = new HashMap<Integer, Fragment>();
		fragments.put(0, miaopingF);
		fragments.put(1, dingshiF);
		fragments.put(2, hushuaF);
		fragments.put(3, dashijieF);
		transaction = supportFragmentManager.beginTransaction();
		if (!fragments.get(0).isAdded()) {
			transaction.add(R.id.fragment_container, fragments.get(index), fragmentTags[index]);
		}

		transaction.show(miaopingF);
		transaction.commit();
		Log.i("ainio", "fragments+++++++++" + fragments.size());
	}

	private void initView() {
		container = (FrameLayout) findViewById(R.id.fragment_container);
		mTabs = new ImageView[4];
		mTabs[0] = (ImageView) findViewById(R.id.miaoping);
		mTabs[1] = (ImageView) findViewById(R.id.dingshi);
		mTabs[2] = (ImageView) findViewById(R.id.hushua);
		mTabs[3] = (ImageView) findViewById(R.id.dashijie);
		mTabs[0].setSelected(true);
	}

	public void onTabSelect(View view) {
		if (view != null) {
			switch (view.getId()) {
			case R.id.miaoping:
				index = 0;
				break;
			case R.id.dingshi:
				index = 1;
				break;
			case R.id.hushua:
				index = 2;
				break;
			case R.id.dashijie:
				index = 3;
				break;
			}
		}
		FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
		if (currentTabIndex != index) {
			if (index > currentTabIndex) {
				trx.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
			} else {
				trx.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
			}
			trx.hide(fragments.get(currentTabIndex));
			if (!fragments.get(index).isAdded()) {
				trx.add(R.id.fragment_container, fragments.get(index), fragmentTags[index]);
			}
			trx.show(fragments.get(index)).commit();
		}
		mTabs[currentTabIndex].setSelected(false);
		mTabs[index].setSelected(true);
		currentTabIndex = index;

	}

	// 第二种方法（再按一次退出程序）
	private long exitTime = 0;
	private Intent data = null;
	private FragmentManager supportFragmentManager;
	private FragmentTransaction transaction;
	private boolean ispaizhao;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				new SPUtil(this).putBoolean("isLogin", false);
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {

	}

	// 将数据保存到outState对象中, 该对象会在重建activity时传递给onCreate方法
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("callbaclCount", ++callbaclCount);
	}


	@Override
	public void change(boolean a) {
		if (a) {
			FragmentTransaction transaction1 = supportFragmentManager.beginTransaction();
			mTabs[0].setSelected(false);
			mTabs[1].setSelected(true);
			index = 1;
			currentTabIndex = index;

			Set<Integer> keys = fragments.keySet();
			for (Integer key : keys) {
				if (fragments.get(key).isAdded()) {
					transaction1.hide(fragments.get(key));
				}
			}
			transaction1.show(dingshiF);

			transaction1.commit();
			// transaction.commit();
		}
	}
}
